package org.nokatag.tagjava;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.nokatag.system.BugInfoWrint;
/**
 * 超连接标签,经过encodeURL编码，支持urlrewrite
 * @author rebin
 * @2015-09-18
 */
public class ALink  extends Inputsuper{
	private static final long serialVersionUID = 8638315838816487444L;
	private String href = null;//地址
	
	
	public int doStartTag()throws JspException{
		JspWriter jspOut = pageContext.getOut();
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		HttpServletResponse response=(HttpServletResponse) pageContext.getResponse();
		String url = href;
		if(null==url || url.trim().length()<5){
			return EVAL_BODY_INCLUDE;
		}
		if(!url.startsWith("http://")){
			if(url.startsWith("/"))
				url = basepath(request)+url;
			else
				url = basepath(request)+"/"+url;
		}
		NOOUT.put("value", true);
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append("<a href=\""+response.encodeURL(url)+"\"");
		stringBuilder.append(getBaseStr()+">");
		try {
				
			jspOut.write(stringBuilder.toString());
		} catch (IOException e) {
			BugInfoWrint.Print(e);
		}
		return EVAL_BODY_INCLUDE;
	}
	public int doEndTag() throws JspException {
		try {
			if(null==href || href.trim().length()<5){
				return EVAL_PAGE;
			}
			StringBuilder body = new StringBuilder();
			body.append("</a>");
			pageContext.getOut().write(body.toString());
			} catch (IOException e) {
				e.printStackTrace();
			}
		return EVAL_PAGE;
	}
	
	public String getHref() {
		return href;
	}

	public void setHref(String href) {
		this.href = href;
	}

}